package com.sctvcloud.bazhong.base;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.ruihang.generalibrary.ui.fragment.BaseGeneraFragment;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.application.Constances;
import com.sctvcloud.bazhong.beans.IntegralAddBean;
import com.sctvcloud.bazhong.http.AbsNetCallBack;
import com.sctvcloud.bazhong.http.NetUtils;
import com.sctvcloud.bazhong.utils.UserManager;

/**
 * Created by liuyu on 2017/8/10.
 */

public class BaseFragment extends BaseGeneraFragment {

    protected boolean isCoverStatusColor = true;
    protected boolean isThisShowing;

    protected int userExpiredCode;

    protected static String rebllionUrl;

    public static void setRebllionUrl(String rebllionUrl) {
        BaseFragment.rebllionUrl = rebllionUrl;
    }

    public static String getRebllionUrl() {
        return rebllionUrl;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isThisShowing) {
            setThisFragmentShowing();
        }
    }

    @Override
    public void onPause() {
        isThisShowing = false;
        super.onPause();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isThisShowing = isVisibleToUser;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       String a = this.getOwnerName();
//        if(isCoverStatusColor) {
//            StatusBarCompat.setStatusBarColor(getActivity(), getResources().getColor(R.color.white));
//        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isThisShowing = !hidden;
    }

    /**
     * 增加积分
     * 1:签到;2:评论;3:转发;4:投稿;5:采用投稿;6:邀请码分享;7:新用户注册;8:输入邀请码
     *
     * @param type
     */
    protected void addInvitation(int type) {
        if (null != UserManager.getInstance().getUser()) {
            ArrayMap<String, Object> p = new ArrayMap<>();
            p.put("ruleType", type);
            p.put("userId", UserManager.getInstance().getUser().getPhoneNumber());
            p.put("token", UserManager.getInstance().getUser().getToken());
            NetUtils.getNetAdapter().postAddIntegral(getOwnerName(), p, new AbsNetCallBack<IntegralAddBean>(IntegralAddBean.class) {
                @Override
                public void onSuc(IntegralAddBean result) {
                    String addMsg = "";
                    switch (result.getRuleType()) {
                        case Constances.ADD_INTEGRAL_SIGN_IN:
                            addMsg = " 签到成功，奖励" + result.getIntegral() + "积分~";
                            break;
                        case Constances.ADD_INTEGRAL_COMMENT:
                            addMsg = " 评论成功，奖励" + result.getIntegral() + "积分~";
                            break;
                        case Constances.ADD_INTEGRAL_FORWARD:
                            addMsg = " 转发成功，奖励" + result.getIntegral() + "积分~";
                            break;
                        case Constances.ADD_INTEGRAL_SUBMISSION:
                            addMsg = " 投稿成功，奖励" + result.getIntegral() + "积分~";
                            break;
                        case Constances.ADD_INTEGRAL_USE_SUBMISSION:
                            addMsg = " 投稿被采用，奖励" + result.getIntegral() + "积分~";
                            break;
                        case Constances.ADD_INTEGRAL_SHARE_INVITATION:
                            addMsg = " 分享邀请码成功，奖励" + result.getIntegral() + "积分~";
                            break;
                        case Constances.ADD_INTEGRAL_REGISTER:
                            addMsg = " 注册成功，奖励" + result.getIntegral() + "积分~";
                            break;
                        case Constances.ADD_INTEGRAL_INPUT_INVITATION:
                            addMsg = " 提交邀请码成功，奖励" + result.getIntegral() + "积分~";
                            break;
                        case Constances.ADD_INTEGRAL_READ_NEWS:
                            addMsg = " 阅读新闻，奖励" + result.getIntegral() + "积分~";
                            break;
                        case Constances.ADD_INTEGRAL_DOUP:
                            addMsg = " 点赞成功，奖励" + result.getIntegral() + "积分~";
                            break;
                    }
                    if (!TextUtils.isEmpty(addMsg)) {
                        toastInCenter(addMsg);
                    }

                }

                @Override
                public void onEnd() {
                    super.onEnd();

                }

                @Override
                public void onError(Throwable ex, String errorMsg) {
                    super.onError(ex, errorMsg);
                }
            });
        }

    }

    public void toastInCenter(String str) {
        Toast toast = Toast.makeText(getContext(), str, Toast.LENGTH_SHORT);
        View view = toast.getView();
        if (view != null) {
//            TextView tv = (TextView) view.findViewById(com.android.internal.R.id.message);
            TextView tv = (TextView) view.findViewById(Resources.getSystem().getIdentifier("message", "id", "android"));
            if (tv != null) {
                tv.setTextColor(Color.WHITE);
                tv.setTextSize(15);
            }
            view.setBackgroundResource(R.drawable.bg_corners_black_toast);
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    protected void setThisFragmentShowing() {
//        JLog.e("ddac  will set Fragment to showing");
        isThisShowing = true;
        UserManager.setActiveFragment(this);
    }

    public boolean isThisShowing() {
        return isThisShowing;
    }

    public void onUserExpired(int code) {
        if (userExpiredCode != code) {
            userExpiredCode = code;
            onUserExpired();
        }
    }

    protected void onUserExpired() {
    }

    public boolean hasShowPlayer() {
        return false;
    }

    public void willDoExit() {
    }
}
